package com.mollytask.superadmin;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.mollytask.R;
import com.mollytask.bean.Institution;
import com.mollytask.bean.Message;
import com.mollytask.bean.MyUser;
import com.mollytask.util.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class DataRemindActivity extends AppCompatActivity {

    private LinearLayout mBackLl;
    private RecyclerView mRemindList;

    private Button mRemindBtn;

    private MyUser myUser;

    private List<Institution> mInstitutions = new ArrayList<>();

    private RemindAdapter mAdapter;

    private int year;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_review);

        year = getIntent().getIntExtra("year", 2000);

        mBackLl = (LinearLayout) findViewById(R.id.ll_back_review);
        myUser = BmobUser.getCurrentUser(MyUser.class);

        mRemindBtn = (Button) findViewById(R.id.btn_remind);
        mRemindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRemindMessage();
            }
        });

        initRecyclerView();
        initData();

        mBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initData() {
        //查找当前行政机构下属哪些公共机构
        BmobQuery<Institution> query = new BmobQuery<>();
        query.addWhereEqualTo("mInSuperAccount", myUser.getCode());
        query.findObjects(new FindListener<Institution>() {
            @Override
            public void done(List<Institution> list, BmobException e) {
                if (e == null) {
                    mInstitutions = list;
                    if (mAdapter == null) {
                        mAdapter = new RemindAdapter(R.layout.item_remind, mInstitutions, year + "");
                        mRemindList.setAdapter(mAdapter);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRemindList = (RecyclerView) findViewById(R.id.remind_recycler);
        mRemindList.setLayoutManager(manager);
    }

    private void sendRemindMessage() {
        for (int i = 0; i < mInstitutions.size(); i++) {
            Message remind = new Message();
            remind.setType(0);
            remind.setmToCode(mInstitutions.get(i).getmInCode());
            remind.setmFromCode(myUser.getCode());
            remind.setmContent("请提交" + year + "年的数据");
            remind.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                }
            });
            DialogUtils.showToast(DataRemindActivity.this, "催报成功");
            finish();
        }
    }
}
